#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include <ctype.h> /* tolower */
#include <string.h>
#include "tools.h"
#include "font.h"
#include "debug.h"


int			max2int(int				x,
				int				y)
{
  if (x > y)
    return (x);
  return (y);
}

void			display_frame(WINDOW			*w)
{
  wborder(w, '|', '|', '-', '-',
	  ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
}


void			display_name(p_context			c,
				     int			i)
{
  mvwprintw(WIN[i], 0,
	    (WIDTH[i] / 2) - (strlen(DESCR[i]) * c->width_font) / 2, DESCR[i]);
}



int			get_font_state(p_context		c)
{
  int			max_line, max_col;
  int			resolution;

  getmaxyx(stdscr, max_line, max_col);
  resolution = max_line * max_col;

  if (resolution < FONT_THIN)
  {
    FONT_SIZE = 1;
    return (0);
  }

  if (resolution < FONT_MEDIUM)
  {
    FONT_SIZE = FONT_SIZE_MEDIUM;
    return (1);
  }

  FONT_SIZE = FONT_SIZE_MASSIVE;
  return (2);
}


void			display_text(p_context			c,
				     int			zone,
				     int			offset_line,
				     int			offset_col,
				     char			*s)
{
  int			bold = 0;
  int			line = 0;
  int			size_char = FONT_SIZE;
  int			nb_char = 0;
  int			max_line, max_col, i;
  int			resolution;
  WINDOW		*w;

  getmaxyx(stdscr, max_line, max_col);
  resolution = max_line * max_col;

  if (resolution < FONT_THIN)
  {
    mvwprintw(WIN[zone], offset_line, offset_col, s);
    refresh();
    return;
  }

  if (0/* (max_line * max_col) > FONT_MEDIUM */)
  {
    /* mvwprintw(WIN[zone], offset_line, offset_col, s); */
    bold = 1;
    size_char = FONT_SIZE_BOLD;
  }

  if (0/* (max_line * max_col) > FONT_MAX */)
  {
    /* mvwprintw(WIN[zone], offset_line, offset_col, s); */
    bold = 2;
    size_char = FONT_SIZE_MASSIVE;
  }

  /* offset_line *= size_char; */
  line = 0;/* offset_line * size_char; */
  /* offset_col *= size_char; */

  for (i = 0; s[i]; s++)
  {
    if (s[i] == '\n')
    {
      line += size_char;
      nb_char = 0;
      offset_col = 0;
      continue;
    }

    if (s[i] == ' ')
    {
      nb_char ++;
      continue;
    }

    /* if (nb_char * size_char > WIDTH[zone]) */
    /*   /\* grepme print ... *\/ */
    /*   return; */

    w = derwin(WIN[zone], size_char, size_char, line + IS_FRAME, nb_char * size_char + IS_FRAME);
    mvwaddstr(w, 0, 0, char2aa(s[i], bold));

    offset_col += size_char + 2;

    /* wrefresh(w); /\* grepme *\/ */
    delwin(w);
    nb_char ++;
  }
}






char			*char2aa(char				c,
				 int				bold)
{
  c = tolower(c); /* grepme */

  switch (c)
  {
  case 'a':
    switch (bold)
    {
    case 0:
      return (FONT_A);
    case 1:
      return (FONT_BOLD_A);
    default:
      return (A_MASSIVE);
    }

  case 'b':
    switch (bold)
    {
    case 0:
      return (FONT_B);
    case 1:
      return (FONT_BOLD_B);
    default:
      return (B_MASSIVE);
    }

  case 'c':
    if (bold)
      return (FONT_BOLD_C);
    return (FONT_C);

  case 'd':
    if (bold)
      return (FONT_BOLD_D);
    return (FONT_D);

  case 'e':
    if (bold)
      return (FONT_BOLD_E);
    return (FONT_E);

  case 'f':
    if (bold)
      return (FONT_BOLD_F);
    return (FONT_F);

  case 'g':
    if (bold)
      return (FONT_BOLD_G);
    return (FONT_G);

  case 'h':
    if (bold)
      return (FONT_BOLD_H);
    return (FONT_H);

  case 'i':
    if (bold)
      return (FONT_BOLD_I);
    return (FONT_I);

  case 'j':
    if (bold)
      return (FONT_BOLD_J);
    return (FONT_J);

  case 'k':
    if (bold)
      return (FONT_BOLD_K);
    return (FONT_K);

  case 'l':
    if (bold)
      return (FONT_BOLD_L);
    return (FONT_L);

  case 'm':
    if (bold)
      return (FONT_BOLD_M);
    return (FONT_M);

  case 'n':
    if (bold)
      return (FONT_BOLD_N);
    return (FONT_N);

  case 'o':
    if (bold)
      return (FONT_BOLD_O);
    return (FONT_O);

  case 'p':
    if (bold)
      return (FONT_BOLD_P);
    return (FONT_P);

  case 'q':
    if (bold)
      return (FONT_BOLD_Q);
    return (FONT_Q);

  case 'r':
    if (bold)
      return (FONT_BOLD_R);
    return (FONT_R);

  case 's':
    if (bold)
      return (FONT_BOLD_S);
    return (FONT_S);

  case 't':
    if (bold)
      return (FONT_BOLD_T);
    return (FONT_T);

  case 'u':
    if (bold)
      return (FONT_BOLD_U);
    return (FONT_U);

  case 'v':
    if (bold)
      return (FONT_BOLD_V);
    return (FONT_V);

  case 'w':
    if (bold)
      return (FONT_BOLD_W);
    return (FONT_W);

  case 'x':
    if (bold)
      return (FONT_BOLD_X);
    return (FONT_X);

  case 'y':
    if (bold)
      return (FONT_BOLD_Y);
    return (FONT_Y);

  case 'z':
    if (bold)
      return (FONT_BOLD_Z);
    return (FONT_Z);

  /* case '0': */
  /*   if (bold) */
  /*     return (FONT_BOLD_0); */
  /*   return (FONT_0); */

  /* case '1': */
  /*   if (bold) */
  /*     return (FONT_BOLD_1); */
  /*   return (FONT_1); */

  /* case '2': */
  /*   if (bold) */
  /*     return (FONT_BOLD_2); */
  /*   return (FONT_2); */

  /* case '3': */
  /*   if (bold) */
  /*     return (FONT_BOLD_3); */
  /*   return (FONT_3); */

  /* case '4': */
  /*   if (bold) */
  /*     return (FONT_BOLD_4); */
  /*   return (FONT_4); */

  /* case '5': */
  /*   if (bold) */
  /*     return (FONT_BOLD_5); */
  /*   return (FONT_5); */

  /* case '6': */
  /*   if (bold) */
  /*     return (FONT_BOLD_6); */
  /*   return (FONT_6); */

  /* case '7': */
  /*   if (bold) */
  /*     return (FONT_BOLD_7); */
  /*   return (FONT_7); */

  /* case '8': */
  /*   if (bold) */
  /*     return (FONT_BOLD_8); */
  /*   return (FONT_8); */

  /* case '9': */
  /*   if (bold) */
  /*     return (FONT_BOLD_9); */
  /*   return (FONT_9); */

  case '?':
    if (bold)
      return (FONT_BOLD_QM);
    return (FONT_QM);

  case '!':
    if (bold)
      return (FONT_BOLD_EM);
    return (FONT_EM);

  case '#':
    if (bold)
      return (FONT_BOLD_SHARP);
    return (FONT_SHARP);

  case ',':
    if (bold)
      return (FONT_BOLD_COMA);
    return (FONT_COMA);

  case '.':
    if (bold)
      return (FONT_BOLD_DOT);
    return (FONT_DOT);


  default:
    return (FONT_SPACE);
  }
}
